import { defineStore } from 'pinia';
import { lsGetItem, lsSetItem } from '@/utils/storage';
import router from '../router';

const TAGS_VIEW = 'tagsView';
const BREADCRUMB_VISIBLE = 'showBreadcrumb';
const TAGSVIEW_VISIBLE = 'showTagsView';
const IS_EXPERIENCE_MODE = 'isExperienceMode';
const TAG_RIGHT = 0;
const TAG_LEFT = 1;
const TAG_CURRENT = 2;
const TAG_OTHER = 3;

const useAppStore = defineStore('app', {
  state: () => {
    return {
      tagsViewList: lsGetItem(TAGS_VIEW) || [],
      excludeCachePage: ['Redirect'],
      showBreadcrumb: lsGetItem(BREADCRUMB_VISIBLE) || true,
      showTagsView: lsGetItem(TAGSVIEW_VISIBLE) || true,
      isExperienceMode: import.meta.env.VITE_IS_EXPERIENCE_MODE || lsGetItem(IS_EXPERIENCE_MODE) || false,
    };
  },
  getters: {},
  actions: {
    /**
     * 转换体验模式
     *
     * @param {boolean} isOpen 是否开启体验模式
     */
    toggleExperienceMode(isOpen) {
      this.isExperienceMode = isOpen;
      lsSetItem(IS_EXPERIENCE_MODE, isOpen);
    },
    /**
     * 设置是否显示面包屑导航
     *
     * @param {boolean} visible 是否显示
     */
    changeBreadNavShow(visible) {
      this.showBreadcrumb = visible;
      lsSetItem(BREADCRUMB_VISIBLE, visible);
    },
    /**
     * 设置是否显示页签
     *
     * @param {boolean} visible 是否可见
     */
    changeTagsViewShow(visible) {
      this.showTagsView = visible;
      lsSetItem(TAGSVIEW_VISIBLE, visible);
    },
    /**
     * 添加缓存的组件
     *
     * @param {string} name 组件名称
     * @returns undefined
     */
    addExcludePage(name) {
      if (this.excludeCachePage.includes(name)) {
        return;
      }
      this.excludeCachePage.push(name);
    },
    /**
     * 移除缓存的组件
     *
     * @param {string} name 组件名称
     */
    removeExcludePage(name) {
      if (this.excludeCachePage.includes(name)) {
        this.excludeCachePage = this.excludeCachePage.filter((item) => item !== name);
      }
    },
    /**
     * 页签刷新功能
     *
     * @param {number} index tag数组索引
     */
    refreshPage(index) {
      const tagView = this.tagsViewList[index];

      this.addExcludePage(tagView.name);
      router.replace({
        path: '/redirect' + tagView.fullPath,
      });
    },

    /**
     * 关闭页面
     *
     * @param {number} type 关闭指令类型
     * @param {number} index 要关闭的页签索引
     * @param {boolean} isCurrent 操作的是否是当前页签
     */
    removeTagsView(type, index, isCurrent) {
      if (type === TAG_CURRENT && index) {
        // 关闭当前
        this.addExcludePage(this.tagsViewList[index].name);
        if (isCurrent) {
          router.push(this.tagsViewList[index - 1].fullPath);
        }
        this.tagsViewList.splice(index, 1);
      } else if (type === TAG_LEFT && index) {
        // 关闭左侧
        // todo... 关闭的添加到不能缓存的列表中
        !isCurrent && router.push(this.tagsViewList[index].fullPath);
        this.tagsViewList.splice(1, index - 1);
      } else if (type === TAG_RIGHT) {
        // 关闭右侧
        // todo... 关闭的添加到不能缓存的列表中
        !isCurrent && router.push(this.tagsViewList[index].fullPath);
        this.tagsViewList.splice(index + 1);
      } else if (type === TAG_OTHER) {
        // 关闭其他
        // todo... 关闭的添加到不能缓存的列表中
        !isCurrent && router.push(this.tagsViewList[index].fullPath);
        this.tagsViewList.splice(index + 1);
        this.tagsViewList.splice(1, index - 1);
      } else {
        // 关闭所有标签页
        // todo... 关闭的添加到不能缓存的列表中
        router.push(this.tagsViewList[0].fullPath);
        this.tagsViewList.splice(1);
      }

      lsSetItem(TAGS_VIEW, this.tagsViewList);
    },
    /**
     * 添加标签页
     *
     * @param {object} tag 页签对象
     */
    addTagsView(tag) {
      const { path, name } = tag;
      const isExisted = this.tagsViewList.find((item) => item.path === path);
      if (!isExisted) {
        this.tagsViewList.push(tag);
        lsSetItem(TAGS_VIEW, this.tagsViewList);
      }

      this.removeExcludePage(name);
    },
  },
});

export default useAppStore;
